package com.jxt.myjxtmanager.ui.fragment;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jxt.myjxtmanager.BaseFragment;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.adapter.BaseAdapterHelper;
import com.jxt.myjxtmanager.adapter.QuickAdapter;
import com.jxt.myjxtmanager.db.MessageTaskModel;
import com.jxt.myjxtmanager.db.Message_task;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.entity.MessageListBean;
import com.jxt.myjxtmanager.utils.TimeCalculate;
import com.jxt.myjxtmanager.views.CustomProgressDialog;

import java.util.List;

/**
 * Created by qm on 2017/3/6 0006.
 */
public class HomeWorkFragment extends BaseFragment {

    private ListView list_frag_homework;
    private String str = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"late_num\": 5,\n" +
            "    \"result\": [\n" +
            "        {\n" +
            "            \"un_read_message\":1,\n" +
            "            \"sale_id\":1489387376925,\n" +
            "            \"title\":\"MyFrameForMVP\",\n" +
            "            \"msg_type\":1,\n" +
            "            \"content\":\"晚上9点半前睡觉\",\n" +
            "            \"status\":1,\n" +
            "            \"send_time\":\"1489405448361\",\n" +
            "            \"id\":187826692\n" +
            "            },\n" +
            "        {\n" +
            "            \"un_read_message\":1,\n" +
            "            \"sale_id\":1489387376926,\n" +
            "            \"title\":\"MyFrameForMVP\",\n" +
            "            \"msg_type\":1,\n" +
            "            \"content\":\"回家不要看电视，打扫房间\",\n" +
            "            \"status\":1,\n" +
            "            \"send_time\":\"1489405448361\",\n" +
            "            \"id\":187826692\n" +
            "            },\n" +
            "        {\n" +
            "            \"un_read_message\":1,\n" +
            "            \"sale_id\":1489387376927,\n" +
            "            \"title\":\"MyFrameForMVP\",\n" +
            "            \"msg_type\":1,\n" +
            "            \"content\":\"和爸妈聊天，给爸爸妈妈讲10个笑话，并且要有5个以上让爸爸妈妈笑\",\n" +
            "            \"status\":1,\n" +
            "            \"send_time\":\"1489405448361\",\n" +
            "            \"id\":187826692\n" +
            "            },\n" +
            "        {\n" +
            "            \"un_read_message\":1,\n" +
            "            \"sale_id\":1489387376928,\n" +
            "            \"title\":\"MyFrameForMVP\",\n" +
            "            \"msg_type\":1,\n" +
            "            \"content\":\"以上三个做好，这个就不用做了\",\n" +
            "            \"status\":1,\n" +
            "            \"send_time\":\"1489405448361\",\n" +
            "            \"id\":187826692\n" +
            "            }\n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";
    private List<Message_task> message_tasks;
    private SaleUser saleUser;

    private CustomProgressDialog dialog;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homework;
    }

    @Override
    protected void initView() {
        list_frag_homework = (ListView) mView.findViewById(R.id.list_frag_homework);
        saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
        message_tasks = MessageTaskModel.getInstance().getMessageByMsgType(1);
        mGson = new Gson();
        dialog = new CustomProgressDialog(getActivity(), "");
        loadData();


    }

    private Gson mGson;

    private void loadData() {
        if (dialog != null) {
            dialog.showDialog();
        }
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (message_tasks == null || message_tasks.size() <= 0) {
                if (mGson != null) {
                    MessageListBean mResult = mGson.fromJson(str, MessageListBean.class);
                    message_tasks = mResult.getResult();
                }
            }
            MessageTaskModel.getInstance().getMessageByDriverIDAndUnRead(saleUser.getUser_id() + "");
            HomeWorkAdapter homeWorkAdapter = new HomeWorkAdapter(getActivity(), R.layout.item_message, message_tasks);
            list_frag_homework.setAdapter(homeWorkAdapter);
            dismissDialog();
        }
    };

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    class HomeWorkAdapter extends QuickAdapter<Message_task> {

        public HomeWorkAdapter(Context context, int layoutId,
                               List<Message_task> datas) {
            super(context, layoutId, datas);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void convert(BaseAdapterHelper helper, Message_task item) {
            // TODO Auto-generated method stub
            helper.setText(R.id.tv_message_update_content, item.getContent());
            helper.setText(R.id.tv_message_updte_date, TimeCalculate.getFormatTimeAll(Long.valueOf(item.getSend_time())));
        /*helper.setText(R.id.tv_chat_title, item.getTitle());
        helper.setText(R.id.tv_chat_content, item.getContent());*/
        }

    }


}
